package com.example.exercise.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import com.example.exercise.sys.vo.SysDictVO;
import com.example.exercise.sys.service.SysDictService;
import com.example.exercise.common.base.BaseResponse;

/**
* 系统字典表
*/
@RestController
@RequestMapping("/sysDict")
@Tag(name = "", description = "")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @Operation(summary = "分页查询列表", description = "分页查询列表")
    @RequestMapping(value = "findByWhere", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Page<SysDictVO>> findByWhere(@RequestBody SysDictVO sysDictVO, Page<SysDictVO> page){
        BaseResponse<Page<SysDictVO>> baseResponse = new BaseResponse<>();
        List<SysDictVO> list = sysDictService.findByWhere(page, sysDictVO);
        page.setRecords(list);
        baseResponse.ok(page);
        return baseResponse;
    }

    @Operation(summary = "根据id查详情", description = "根据id查详情")
    @RequestMapping(value = "findByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<SysDictVO> findByKey(@RequestBody SysDictVO sysDictVO){
        BaseResponse<SysDictVO> baseResponse = new BaseResponse<>();
        SysDictVO sysDictVO1 = sysDictService.findByKey(sysDictVO);
        if(sysDictVO1 == null){
            baseResponse.errMsg("数据为空");
        }else{
            baseResponse.ok(sysDictVO1);
        }
        return baseResponse;
    }

    @Operation(summary = "添加一条记录", description = "添加一条记录")
    @RequestMapping(value = "insertSysDict", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> insertSysDict(@RequestBody SysDictVO sysDictVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = sysDictService.insert(sysDictVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("新增数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "修改一条记录", description = "修改一条记录")
    @RequestMapping(value = "updateSysDictByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> updateSysDictByKey(@RequestBody SysDictVO sysDictVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = sysDictService.updateByKey(sysDictVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("修改数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "删除一条记录", description = "删除一条记录")
    @RequestMapping(value = "deleteByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> deleteByKey(@RequestBody SysDictVO sysDictVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = sysDictService.deleteByKey(sysDictVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("删除数据失败");
        }
        return baseResponse;
    }

}